package com.symone.webcms.util;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.CRF.CRFSegment;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.suggest.Suggester;
import com.hankcs.hanlp.tokenizer.SpeedTokenizer;
import com.symone.webcms.model.Menu;
import org.mindrot.jbcrypt.BCrypt;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {
    public static final char UNDERLINE = '_';

    public static void main(String[] args){
        System.out.println(encodePassword("123456"));

        //文本推荐
        Suggester suggester = new Suggester();
        String[] titleArray =(
                "威廉王子发表演说 呼吁保护野生动物\n" +
                "《时代》年度人物最终入围名单出炉 普京马云入选\n" +
                "“黑格比”横扫菲：菲吸取“海燕”经验及早疏散\n" +
                "日本保密法将正式生效 日媒指其损害国民知情权\n" +
                "英报告说空气污染带来“公共健康危机”"
        ).split("\\n");
        for (String title : titleArray)
            suggester.addSentence(title);
        //根据语义的推荐   通过IdVectorScorer(单词语义向量打分器)实现
        System.out.println(suggester.suggest("发言", 2));
        //根据字符的推荐   通过EditDistanceScorer(编辑距离打分器 )实现
        System.out.println(suggester.suggest("危机公共", 1));
        //根据拼音的推荐   通过PinyinScorer(拼音打分器)实现
        System.out.println(suggester.suggest("mayun", 1));

        //极速词典分词
        String text1 = "泽田依子是上外日本文化经济学院的外教";
        Segment segment = HanLP.newSegment();;
        segment.enablePartOfSpeechTagging(true)
//               .enableCustomDictionary(false)
//               .enablePlaceRecognize(true)
//               .enableOrganizationRecognize(true)
               .enableAllNamedEntityRecognize(true);
        System.out.println(segment.seg(text1));

        String text2 = "江西鄱阳湖干枯，中国最大淡水湖变成大草原";
        System.out.println(segment.seg(text2));
        System.out.println(SpeedTokenizer.segment(text2));
        long start = System.currentTimeMillis();
        int pressure = 1000000;
        for (int i = 0; i < pressure; ++i)
        {
            SpeedTokenizer.segment(text2);
        }
        double costTime = (System.currentTimeMillis() - start) / (double)1000;
        System.out.printf("分词速度：%.2f字每秒", text2.length() * pressure / costTime);

        //关键词提取
        String content = "程序员(英文Programmer)是从事程序开发、维护的专业人员。一般将程序员分为程序设计人员和程序编码人员，但两者的界限并不非常清楚，特别是在中国。软件从业人员分为初级程序员、高级程序员、系统分析员和项目经理四大类。";
        List<String> keywordList = HanLP.extractKeyword(content, 5);
        System.out.println(keywordList);

        //自动摘要
        String document = "算法可大致分为基本算法、数据结构的算法、数论算法、计算几何的算法、图的算法、动态规划以及数值分析、加密算法、排序算法、检索算法、随机化算法、并行算法、厄米变形模型、随机森林算法。\n" +
                "算法可以宽泛的分为三类，\n" +
                "一，有限的确定性算法，这类算法在有限的一段时间内终止。他们可能要花很长时间来执行指定的任务，但仍将在一定的时间内终止。这类算法得出的结果常取决于输入值。\n" +
                "二，有限的非确定算法，这类算法在有限的时间内终止。然而，对于一个（或一些）给定的数值，算法的结果并不是唯一的或确定的。\n" +
                "三，无限的算法，是那些由于没有定义终止定义条件，或定义的条件无法由输入的数据满足而不终止运行的算法。通常，无限算法的产生是由于未能确定的定义终止条件。";
        List<String> sentenceList = HanLP.extractSummary(document, 3);
        System.out.println(sentenceList);
    }

    public static String encodePassword(String password){
        return BCrypt.hashpw(password, BCrypt.gensalt());
    }

    public static boolean checkPassword(String password, String hashed){
        return BCrypt.checkpw(password,hashed);
    }

    public static List<Menu> list2Tree(List<Menu> treeList) {
        List<Menu> retList = new ArrayList<>();
        for (Menu root : treeList) {
            if (root.getPid() == 0 ) {
                retList.add(findChildren(root, treeList));
            }
        }
        return retList;
    }

    private static Menu findChildren(Menu parent, List<Menu> treeList) {
        for (Menu child : treeList) {
            if (parent.getId().equals(child.getPid()) ) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(findChildren(child, treeList));
            }
        }
        return parent;
    }

    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        StringBuilder sb = new StringBuilder(param);
        Matcher mc = Pattern.compile("_").matcher(param);
        int i = 0;
        while (mc.find()) {
            int position = mc.end() - (i++);
            sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
        }
        return sb.toString();
    }

    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

}
